import RouterConfig from "./index";
import RouteBeforeConfig from "./RouteBeforeConfig"
import { Alert } from "antd"

// 模拟导航首位
// 该组件也是一个容器组件，不做任何jsx的展示，完全是为了实现一些功能


const RouteBefore = () => {

    // 关闭alert的回调
    const closeHandle = () => {
        // eslint-disable-next-line no-restricted-globals
        location.pathname = '/'
    }

    // 根据location.pathname获取到RouteBeforeConfig所匹配到的对象
    const currentPath = RouteBeforeConfig.filter(item => {
        // eslint-disable-next-line no-restricted-globals
        return item.path === location.pathname
    })[0];

    if (currentPath?.needLogin && !localStorage.getItem("token")) {
        return <Alert
            message="请先登录"
            type="warning"
            closable //支持关闭
            onClose={closeHandle}
            style={{ margin: "30px 0" }}
        />
    }


    return (
        <RouterConfig />
    );
}

export default RouteBefore;